package com.aiwhose.aiwhosesecurity.service.impl;

import cn.hutool.core.map.MapUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.aiwhose.aiwhosesecurity.utils.SMSMonyunUtil;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.aiwhose.aiwhosecore.bean.security.ShortMessage;
import com.aiwhose.aiwhosesecurity.mapper.ShortMessageMapper;
import com.aiwhose.aiwhosesecurity.service.ShortMessageService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * 短信 服务层实现。
 *
 * @author 田国刚
 * @since 1.0.1
 */
@Service
public class ShortMessageServiceImpl extends ServiceImpl<ShortMessageMapper, ShortMessage> implements ShortMessageService {
    @Resource
    private ShortMessageMapper shortMessageMapper;

    @Override
    public boolean sendSMS(String content, String phone) {
        SMSMonyunUtil smsMonyunUtil = new SMSMonyunUtil();
        String smsResult = smsMonyunUtil.sendMessage(phone, content);
        JSONObject entries = JSONUtil.parseObj(smsResult);
        Integer integer = MapUtil.get(entries, "result", Integer.class);
        ShortMessage userSms = new ShortMessage();
        userSms.setContent(content);
        userSms.setPhone(phone);
        userSms.setResultComment(smsResult);
        if (integer == 0) {
            userSms.setSendState(true);
            shortMessageMapper.insert(userSms);
            return true;
        } else {
            userSms.setSendState(false);
            shortMessageMapper.insert(userSms);
            return false;
        }
    }
}
